package com.example.ya12_10;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    List<String> data=new ArrayList<>();
    HomeAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.a1);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(new HomeAdapter());
         data.add("ao");
         data.add("ab");
         data.add("ac");
    }

    class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

           return new MyViewHolder(LayoutInflater.from(MainActivity.this)
                   .inflate(R.layout.cell,viewGroup,false));
        }

        @Override
        public void onBindViewHolder(@NonNull HomeAdapter.MyViewHolder myViewHolder, final int i) {

            //myViewHolder.nick.setText(data.get(i));
            myViewHolder.nick.setText(data.get(i));
            myViewHolder.record.setText("聊天记录待定---");
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    data.remove(i);
                    notifyItemRemoved(i);

                }
            });
        }
        @Override
        public int getItemCount() {
            return data.size();
        }

        class  MyViewHolder extends RecyclerView.ViewHolder{
            TextView nick;
            TextView record;
            public MyViewHolder(View inflate) {
                super(inflate);
                nick=inflate.findViewById(R.id.nick);
                record=inflate.findViewById(R.id.record);

            }

        }
        }
    }